/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package crm.salesOrderProcessing.managedbean;

import commonInfrastructure.accountManagement.entity.SalesmanEntity;
import commonInfrastructure.accountManagement.session.EmployeeSessionBean;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import crm.salesOrderProcessing.entity.LineItemEntity;
import crm.salesOrderProcessing.entity.ProductEntity;
import crm.salesOrderProcessing.entity.SalesInquiryEntity;
import crm.salesOrderProcessing.entity.SalesQuotationEntity;
import crm.salesOrderProcessing.session.SalesInquirySessionBean;
import crm.salesOrderProcessing.session.SalesQuotationSessionBean;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author frankxgz
 */
@ManagedBean
@ViewScoped
public class ToQuotationManagedBean implements Serializable {

    @EJB
    SalesInquirySessionBean inquirySessionBean;
    @EJB
    SalesQuotationSessionBean quotationSessionBean;
    @EJB
    EmployeeSessionBean employee;
    
    private List<SalesInquiryEntity> inquiryList;
    private Long inquiryId;
    private SalesInquiryEntity inquiry;
    private SalesQuotationEntity quotation;
    private List<LineItemEntity> lineItems = new ArrayList<LineItemEntity>();
    private SalesQuotationEntity selectedSalesQuotation;

    /** Creates a new instance of RaiseQuotationManagedBean */
    public ToQuotationManagedBean() {
    }
    
    public void initOne(PhaseEvent event){
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        Long loginId = (Long) request.getSession().getAttribute("userId");
        SalesmanEntity user = employee.getSalesmanById(loginId);
        List<String> userType = new ArrayList<String>();
        for (int i=0;i<user.getRoles().size();i++){
            userType.add(user.getRoles().get(i).getRoleName());
        }
        
        if (userType.contains("Sales Manager")){
            inquiryList = inquirySessionBean.getAllSalesInquiry();
        }
        else{
            inquiryList = inquirySessionBean.getInquiryByUser(user);
        }
    }

    public void addInquiry(SalesInquiryEntity selectedInquiry) throws IOException {
        inquiry = selectedInquiry;
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("exInquiry", getInquiry());
        FacesContext.getCurrentInstance().getExternalContext().redirect("raiseSalesQuotation.xhtml");
    }

    public void goBack() throws IOException {
        FacesContext.getCurrentInstance().getExternalContext().redirect("inquiryToQuotation.xhtml");
    }

    public void initView(PhaseEvent event) {

        if (quotation == null) {
            if (inquiry == null) {
                inquiry = (SalesInquiryEntity) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("exInquiry");
            }
            quotation = new SalesQuotationEntity();
            List<LineItemEntity> lineItemList = inquiry.getInquiryLineItems();

            for (int i = 0; i < lineItemList.size(); i++) {
                getLineItems().add(lineItemList.get(i));
            }



            quotation.setCompany(inquiry.getCompany());

            List<Long> leftProducts = new ArrayList<Long>();
            if (!isIn(Long.valueOf(1L), lineItems)) {
                leftProducts.add(1L);
            }
            if (!isIn(Long.valueOf(2L), lineItems)) {
                leftProducts.add(2L);
            }
            if (!isIn(Long.valueOf(3L), lineItems)) {
                leftProducts.add(3L);
            }
            if (!isIn(Long.valueOf(4L), lineItems)) {
                leftProducts.add(4L);
            }

            for (Object o : leftProducts) {
                Long pId = (Long) o;
                ProductEntity pd = inquirySessionBean.getProduct(pId);
                LineItemEntity li = new LineItemEntity();
                li.setProduct(pd);
                li.setQuantity(0);
                lineItems.add(li);
            }
            quotation.setQuotationLineItems(lineItems);
        }
    }

    private boolean isIn(Long id, List<LineItemEntity> itemList) {
        for (Object o : itemList) {
            LineItemEntity li = (LineItemEntity) o;
            if (id.equals(li.getProduct().getId())) {
                return true;
            }
        }
        return false;
    }

    public void proceed() throws IOException {
        for (int i = 0; i < quotation.getQuotationLineItems().size(); i++) {
            if (quotation.getQuotationLineItems().get(i).getQuantity() == 0) {
                quotation.getQuotationLineItems().remove(i);
                i = i - 1;
            }
        }
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("quotation", quotation);
        FacesContext.getCurrentInstance().getExternalContext().redirect("quotationForConfirm.xhtml");
    }
    
    public void viewInquiry(SalesInquiryEntity selectedSalesInquiry) throws IOException{
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("newInquiry", selectedSalesInquiry);
        FacesContext.getCurrentInstance().getExternalContext().redirect("viewSalesInquiry2.xhtml");
    }

    public Long getInquiryId() {
        return inquiryId;
    }

    public void setInquiryId(Long inquiryId) {
        this.inquiryId = inquiryId;
    }

    public SalesInquiryEntity getInquiry() {
        return inquiry;
    }

    public void setInquiry(SalesInquiryEntity inquiry) {
        this.inquiry = inquiry;
    }

    public SalesQuotationEntity getQuotation() {
        return quotation;
    }

    public void setQuotation(SalesQuotationEntity quotation) {
        this.quotation = quotation;
    }

    public List<LineItemEntity> getLineItems() {
        return lineItems;
    }

    public void setLineItems(List<LineItemEntity> lineItems) {
        this.lineItems = lineItems;
    }

    public SalesQuotationEntity getSelectedSalesQuotation() {
        return selectedSalesQuotation;
    }

    public void setSelectedSalesQuotation(SalesQuotationEntity selectedSalesQuotation) {
        this.selectedSalesQuotation = selectedSalesQuotation;
    }

    /**
     * @return the inquiryList
     */
    public List<SalesInquiryEntity> getInquiryList() {
        return inquiryList;
    }

    /**
     * @param inquiryList the inquiryList to set
     */
    public void setInquiryList(List<SalesInquiryEntity> inquiryList) {
        this.inquiryList = inquiryList;
    }
}